package technion.gc.web;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import technion.gc.api.interfaces.AbstractGraphNode;
import technion.gc.api.interfaces.IGraphNodeAction;


public class WebGraphNodeAction implements IGraphNodeAction {

	@Override
	public void execute(AbstractGraphNode graphNode) {
		graphNode.setNeighbors(null);
		addNeighbours(graphNode);
	}
	
	private void addNeighbours(AbstractGraphNode graphNode) {
		String url = (String)graphNode.getData();
		Document doc = null;
		WebGraphNode webGraphNode = (WebGraphNode)graphNode;
		
		int counter = 3;
		do{
			try {
				doc = Jsoup.connect(url).get();
				break;
			} catch (IOException e) {
				if (e.getMessage().startsWith("Unhandled content type")) {
					//webGraphNode.getLog().error(url + " is a link to a non-html file");
					return;
				}
				if (counter <= 0) {
					return;
				}
				counter--;
			}
		} while (counter > 0);
		
		webGraphNode.getLog().info("Node address: " + webGraphNode.getData());
		if (webGraphNode.getDepth() >= webGraphNode.getMaximumDepth()) {
			return;
		}
		
		Elements links = doc.select("a[href]");
		ArrayList<AbstractGraphNode> newNeighbours = new ArrayList<AbstractGraphNode>();
		WebGraphNode neighbour;
		for (Element link : links) {
			if (!(webGraphNode.getDomainConsistent()) || cleanUrl(link.absUrl("href")).startsWith(webGraphNode.getDomain())) { 
				neighbour = new WebGraphNode(cleanUrl(link.absUrl("href")));
				neighbour.setDepth(webGraphNode.getDepth() + 1);
				neighbour.setDomainConsistent(webGraphNode.getDomainConsistent());
				neighbour.setMaximumDepth(webGraphNode.getMaximumDepth());
				neighbour.setDomain(webGraphNode.getDomain());
				neighbour.setLog(webGraphNode.getLog());
				newNeighbours.add(neighbour);
			}
			
		}
		graphNode.setNeighbors(newNeighbours);
	}
	
	private static String cleanUrl(String absUrl) {
		String url = absUrl.split("#")[0]; // Remove anchors 
		if (url.endsWith("/")) // Remove last "/"
			return url.substring(0, url.length() - 1);
		return url;
	}

}

